package com.hcy.app.screen.cap

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.hcy.app.LocalNavController

internal const val TAG = "cap"

@Composable
@Preview
fun Cap_SettingPre(){
    Box(
        modifier = Modifier.fillMaxSize().background(Color.Cyan)
    ){
//        Cap_Setting()
    }

}


@Composable
fun Cap_Setting(capViewModel: CapViewModel = hiltViewModel()){
    val username = remember { mutableStateOf(TextFieldValue("root")) }
    val password = remember { mutableStateOf(TextFieldValue("3.14")) }
    val apiSuf = remember { mutableStateOf(TextFieldValue("http://hcy.com/api/")) }
    val capState by capViewModel.capState.collectAsState()

    val navController = LocalNavController.current

    Column(
        modifier = Modifier.fillMaxSize().systemBarsPadding()
    ) {
        Row(
            modifier = Modifier.fillMaxWidth().padding(bottom = 10.dp),
            horizontalArrangement = Arrangement.Center,
        ) {
            Text(
                text = "Cap配置信息",
                fontSize =  36.sp
            )
        }
        Row(
            modifier = Modifier.fillMaxWidth().padding(bottom = 10.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = "账户：",
                modifier = Modifier.width(80.dp),
                fontSize = 20.sp
            )
            OutlinedTextField(
                value = username.value,
                onValueChange = { username.value = it },
                placeholder = {Text("请输入cap账户信息")},
                modifier = Modifier.fillMaxWidth(),
            )
        }
        Row(
            modifier = Modifier.fillMaxWidth().padding(bottom = 10.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text="密码：",
                modifier = Modifier.width(80.dp),
                fontSize = 20.sp
            )
            OutlinedTextField(
                value = password.value,
                onValueChange = { password.value = it },
                placeholder = {Text("请输入密码")},
                //设置输入类型，并配合显示掩码方式
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
                //这里使用的是密码类型，默认给...隐藏显示效果
                visualTransformation = PasswordVisualTransformation(),
                modifier = Modifier.fillMaxWidth(),
            )
        }
        Row(
            modifier = Modifier.fillMaxWidth().padding(bottom = 10.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text="地址：",
                modifier = Modifier.width(80.dp),
                fontSize = 20.sp
            )
            OutlinedTextField(
                value = apiSuf.value,
                onValueChange = { apiSuf.value = it },
                placeholder = {Text("请输入服务器地址")},
                modifier = Modifier.fillMaxWidth(),
            )
        }
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
        ) {
            Button(
                onClick = {
                    // 保存数据
                    capViewModel.saveConfig(CapConfig(username.value.text, password.value.text, apiSuf.value.text))
                },
                modifier = Modifier.width(150.dp)
            ) {
                Text("保存")
            }
            Spacer(
                modifier = Modifier.width(20.dp)
            )
            Button(
                onClick = {
                    // 保存数据
                    capViewModel.saveConfig(CapConfig(username.value.text, password.value.text, apiSuf.value.text))
                    capViewModel.capLogin( CapConfig(username.value.text, password.value.text, apiSuf.value.text) ){isSuccess ->{
                        if (isSuccess){
                            navController.navigate("cap/home")
                        }
                    }}
                },
                modifier = Modifier.width(150.dp)
            ) {
                Text("验证")
            }
        }
    }
}

